//
//  MSTopBarNode.m
//  MagicSword
//
//  Created by Netease on 15/11/16.
//  Copyright © 2015年 Jonear. All rights reserved.
//

#import "MSTopBarNode.h"
#import "MSHeroManager.h"
#import "MSHeroAvatarNode.h"
#import "MSDBManager.h"

@implementation MSTopBarNode {
    SKLabelNode *_coinLabelNode;
    SKSpriteNode *_backNode;
    SKSpriteNode *_heroNodes;
    SKLabelNode *_titleLabelNode;
}

- (id)init {
    self = [super initWithColor:UIColorFromRGB(0x292933) size:CGSizeMake(ScreenWidth, TOPBARHEIGHT)];
    if (self) {
        [self initSubNodes];
    }
    return self;
}

- (void)initSubNodes {
    // 商店
    SKSpriteNode *shopNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_shop"];
    shopNode.position = CGPointMake((CGRectGetWidth(self.frame)-CGRectGetWidth(shopNode.frame))/2, -TOPBARHEIGHT/4);
    shopNode.zPosition = 4;
    shopNode.alpha = 0.9;
    shopNode.name = NodeName_ShopNode;
    [self addChild:shopNode];
    
    // 金币
    SKSpriteNode *coinNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_coin"];
    coinNode.position = CGPointMake(CGRectGetMidX(shopNode.frame)-CGRectGetWidth(coinNode.frame)+10, -TOPBARHEIGHT/4);
    coinNode.zPosition = 4;
    [self addChild:coinNode];
    
    _titleLabelNode = [SKLabelNode labelNodeWithFontNamed:@"Arial-BoldItalicMT"];
    _titleLabelNode.color = UIColorFromRGB(0x737373);
    [_titleLabelNode setFontSize:20];
    _titleLabelNode.position = CGPointMake(0, -TOPBARHEIGHT/3);
    _titleLabelNode.zPosition = 4;
    [self addChild:_titleLabelNode];
    
    _coinLabelNode = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
    _coinLabelNode.color = UIColorFromRGB(0x737373);
    [_coinLabelNode setText:@"1,200"];
    [_coinLabelNode setFontSize:12];
    _coinLabelNode.position = CGPointMake(coinNode.position.x+3, coinNode.position.y-8);
    _coinLabelNode.zPosition = 5;
    [self addChild:_coinLabelNode];
    
    // 返回
    _backNode = [SKSpriteNode spriteNodeWithImageNamed:@"main_topBack"];
    _backNode.position = CGPointMake((CGRectGetWidth(_backNode.frame))/2-CGRectGetWidth(self.frame)/2, -TOPBARHEIGHT/4);
    _backNode.zPosition = 4;
    _backNode.hidden = YES;
    _backNode.name = NodeName_BackNode;
    [self addChild:_backNode];
    
    // 英雄
    [self initHeroNodes];
}

- (void)initHeroNodes {
    _heroNodes = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake((30+16)*6, TOPBARHEIGHT)];
    _heroNodes.name = NodeName_HeroNodes;
    _heroNodes.hidden = NO;
    _heroNodes.zPosition = 4;
    [self addChild:_heroNodes];
    
    NSArray *fightHero = [[MSHeroManager shareManager] getFightingHeroArray];
    NSInteger maxHeroCount = [[MSDBManager shareManager] getMaxHeroCount];
    for (int i=0; i<6; i++) {
        NSString *imageName = @"main_defaultAvatar";
        if (i>=maxHeroCount) {
            imageName = @"main_lockAvatar";
        }
        
        SKSpriteNode *heroNode = [SKSpriteNode spriteNodeWithImageNamed:imageName];
        if (fightHero.count > i) {
            heroNode = [[MSHeroAvatarNode alloc] initWithHeroInfo:fightHero[i]];
        }
        heroNode.size = CGSizeMake(30, 30);
        heroNode.position = CGPointMake(i*(CGRectGetWidth(heroNode.frame)+8)+30-CGRectGetWidth(self.frame)/2, -22);
        heroNode.alpha = 0.9;
        heroNode.zPosition = 5;
        heroNode.name = NodeName_HeroNodes;
        [_heroNodes addChild:heroNode];
    }
}

- (void)updateHeroArray {
    if (_heroNodes) {
        [_heroNodes removeFromParent];
        _heroNodes = nil;
    }
    [self initHeroNodes];
}

- (void)showBackNode:(BOOL)isShow {
    _backNode.hidden = !isShow;
    _heroNodes.hidden = isShow;
}

- (void)setTitle:(NSString *)title {
    [_titleLabelNode setText:title];
}

@end
